package cn.yuan.org.controller;

import cn.yuan.basic.util.PageList;
import cn.yuan.org.domain.DictionaryTypeDetail;
import cn.yuan.org.query.DictionaryTypeDetailQuery;
import cn.yuan.org.query.Query;
import cn.yuan.org.service.IDictionaryTypeDetailService;
import cn.yuan.util.AjaxResult.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DictionaryTypeDetailController {
    @Autowired
    private IDictionaryTypeDetailService dictionaryTypeDetailService;

    /**
     * 分页查询
     * @param query
     * @return
     */
    @PostMapping
    public PageList<DictionaryTypeDetail> getDictionaryTypeDetail(@RequestBody Query query){
        System.out.println(query.getCurrentPage());
        if(query.getId() !=null){
            return dictionaryTypeDetailService.getDictionaryTypeDetail(query);
        }else{
            return dictionaryTypeDetailService.getDetail(query);
        }
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult delById(@PathVariable Long id){
        try {
            dictionaryTypeDetailService.delById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络延迟，请稍后重试！");
        }
    }

    /**
     * 增加和修改数据
     * @param dictionaryTypeDetail
     * @return
     */
    @PutMapping
    public AjaxResult save(@RequestBody DictionaryTypeDetail dictionaryTypeDetail){
        System.out.println(dictionaryTypeDetail.getTypes_id());
        try {
            if(dictionaryTypeDetail.getId() == null){
                dictionaryTypeDetailService.save(dictionaryTypeDetail);
            }else{
                dictionaryTypeDetailService.update(dictionaryTypeDetail);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络错误，请稍后重试！");
        }
    }
}
